package poker.beliefs;

import poker.beliefs.*;

/**
 *  Editable Java class for concept <code>Pack</code> of poker ontology.
 */
public class Pack  extends PackData
{
	//-------- constructors --------

	/** 
	 *  Default Constructor. <br>
	 *  Create a new <code>Pack</code>.
	 */
	public Pack() {
		// Empty constructor required for JavaBeans (do not remove).
	}


	/** 
	 *  Clone Constructor. <br>
	 *  Create a new <code>Pack</code>.<br>
	 *  Copy all attributes from <code>proto</code> to this instance.
	 *
	 *  @param proto The prototype instance.
	 */
	public Pack(Pack proto) {
		setCardsUsed(proto.getCardsUsed());
		setPack(proto.getPack());
	}

	//-------- custom code --------

	public void newPack() {
		Card[] aux = new Card[52];
		int cardCt = 0; // How many cards have been created so far.
		for ( int suit = 0; suit <= 3; suit++ ) {
			for ( int value = 2; value <= 14; value++ ) {
				aux[cardCt] = new Card(value,suit);
				cardCt++;
			}
		}
		setPack(aux);
		setCardsUsed(0);
	}

	public void shuffle() {
		// Put all the used cards back into the pack, and shuffle it into
		// a random order.
		Card[] aux = getPack();

		for (int i = 51; i > 0; i--) {
			int rand = (int)(Math.random()*(i+1));
			Card temp = aux[i];
			aux[i] = aux[rand];
			aux[rand] = temp;
		}
		setPack(aux);
		setCardsUsed(0);
	}

	public int cardsLeft() {
		// As cards are dealt from the pack, the number of cards left
		// decreases.  This function returns the number of cards that
		// are still left in the pack.
		return 52 - getCardsUsed();
	}
	
    public Card dealCard() {
    	// Deals one card from the pack and returns it.
    	int cardsUsed = getCardsUsed();
        if (cardsUsed == 52)
           shuffle();
        cardsUsed++;
        setCardsUsed(cardsUsed);
        return getPack(cardsUsed - 1);
    }

	//-------- Object methods -----

	/**
	 *  Get a string representation of this <code>Pack</code>.
	 *  @return The string representation.
	 */
	public String toString() {
		return "Pack("
		+ ")";
	}

	/** 
	 *  Get a clone of this <code>Pack</code>.
	 *  @return a shalow copy of this instance.
	 */
	public Object clone() {
		return new Pack(this);
	}

	/** 
	 *  Test the equality of this <code>Pack</code> 
	 *  and an object <code>obj</code>.
	 *
	 *  @param obj the object this test will be performed with
	 *  @return false if <code>obj</code> is not of <code>Pack</code> class,
	 *          true if all attributes are equal.   
	 */
	public boolean equals(Object obj) {
		if (obj instanceof Pack) {
			Pack cmp=(Pack)obj;
			if (getCardsUsed()!=cmp.getCardsUsed()) return false;
			if (getPack()!=cmp.getPack() &&
					(getPack()==null || !getPack().equals(cmp.getPack()))
			) return false;
			return true;
		}
		return false;
	}
}

